package wca;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import robocode.BattleResults;
import robocode.control.events.BattleCompletedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
import robocode.control.snapshot.ITurnSnapshot;
import robocode.control.testing.RobotTestBed;

/**
 * TestGetClose tests if the robot can get close to the target robot consistently and stay near it
 * until the end of the round.
 * 
 * @author Calvin Wong
 * 
 */

public class TestGetClose extends RobotTestBed {

  // Variable for whether robots are in close proximity to each other.
  private boolean proximity = false;

  /**
   * The number of rounds to test.
   * 
   * @return 10 rounds
   * 
   */
  @Override
  public int getNumRounds() {
    return 10;
  }

  /**
   * Looks at a snapshot of each turn and checks if the robots are close to eachother.
   * 
   * @param e Turn Ended Even object
   * 
   */
  @Override
  public void onTurnEnded(TurnEndedEvent e) {

    // Get a snapshot of the turn.
    ITurnSnapshot snapshot = e.getTurnSnapshot();

    // Get the current robots on the field.
    IRobotSnapshot[] robots = snapshot.getRobots();

    // If the robot is in close proximity of the target robot.
    if (robots[0].getX() > robots[1].getX() - 50 && robots[0].getX() < robots[1].getX() + 50
        && robots[0].getY() > robots[1].getY() - 50 && robots[0].getY() > robots[1].getY() + 50) {
      proximity = true;
    }

    // The robot is not in close proximity.
    else {
      proximity = false;
    }
  }

  /**
   * Looks at the battle completed results and see is the robot was able to stay near the target.
   * 
   * @param e Battle Completed Event object
   * 
   */
  public void onBattleCompleted(BattleCompletedEvent e) {
    assertFalse("Check that the robot is able to find and go to where the target was at ",
        proximity);
    
    BattleResults[] results = e.getIndexedResults();
    BattleResults chinpoGumResults = results[1];
    String robotName = chinpoGumResults.getTeamLeaderName();

    assertEquals("Check that the winner is ChinpoGum", robotName, "wca.ChinpoGum*");
  }

  /**
   * Returns the robots to be used.
   * 
   * @return Robots
   * 
   */
  @Override
  public String getRobotNames() {
    return "sample.Crazy,wca.ChinpoGum";
  }
}
